package cn.cmdr.service.ServiceImpl;

import cn.cmdr.mapper.StaffMapper;
import cn.cmdr.pojo.Staff;
import cn.cmdr.service.StaffService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import cn.cmdr.vo.PageVo;
import cn.cmdr.vo.StaffVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service("staffService")
public class StaffServiceImpl extends ServiceImpl<StaffMapper, Staff> implements StaffService {
    @Resource
    private StaffMapper staffMapper;

    @Override
    public Staff getStaffCode(String staffCode) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq(staffCode!=null,"staff_code",staffCode).eq("whether",1);
        return staffMapper.selectOne(queryWrapper);
    }

    @Override
    public List<Staff> getStaffList() {
        return staffMapper.selectList(null);
    }

    @Override
    public PageVo getPageStaffList(Integer pageNo, Integer pageSize, StaffVo staffVo) {
        Page page = new Page();
        page.setSize(pageSize);
        page.setCurrent(pageNo);
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.like(!StringUtils.isEmpty(staffVo.getStaffCode()),"staff_code",staffVo.getStaffCode())
                .like(!StringUtils.isEmpty(staffVo.getStaffName()),"staff_name",staffVo.getStaffName());
        List<Staff> list = staffMapper.getStaffPageList(page,queryWrapper);
        return new PageVo(list,page.getTotal());
    }

    @Override
    public void add(Staff staff) {
        staffMapper.insert(staff);
    }

    @Override
    public void update(Staff staff) {
        staffMapper.updateById(staff);
    }

    @Override
    public void deleteById(Integer id) {
        staffMapper.deleteById(id);
    }

    @Override
    public Staff getStaffById(Integer id) {
        return staffMapper.selectById(id);
    }

    @Override
    public Staff getStaffByCode(String staffCode) {
        return staffMapper.getStaffByCode(staffCode);
    }

    @Override
    public List<Staff> getById(Integer roleId) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq(roleId!=null,"role_id",roleId);
        return staffMapper.selectList(queryWrapper);
    }
}
